package itbour.onetouchshow.activity.userinfo;

import android.content.Context;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.andview.adapter.ABaseRefreshAdapter;
import com.andview.adapter.BaseRecyclerHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import itbour.onetouchshow.App;
import itbour.onetouchshow.R;
import itbour.onetouchshow.activity.web.CommWebActivity;
import itbour.onetouchshow.base.mvp.MVPBaseActivity;
import itbour.onetouchshow.constants.AppConst;


public class MineXiuMiActivity extends MVPBaseActivity<MineXiuMiContract.View, MineXiuMiPresenter> implements MineXiuMiContract.View {

    @BindView(R.id.tv_xiumi_account)
    TextView tvXiumiAccount;
    @BindView(R.id.ls_record)
    RecyclerView lsRecord;
    @BindView(R.id.id_tv_eq)
    TextView idTvEq;
    @BindView(R.id.id_tv_buy)
    TextView idTvBuy;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_mine_xiumi;
    }


    @Override
    protected void initTitle() {
        super.initTitle();
        setTitleText("我的秀米");
        getToolBarView().setRightText("明细记录").setVisibility(View.VISIBLE);
        getToolBarView().getRightTextView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(XiuMiDetailActivity.getLaunchIntent(MineXiuMiActivity.this, Integer.valueOf(App.getUserId())));
            }
        });

        idTvEq.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG); //下划线
        idTvEq.getPaint().setAntiAlias(true);//抗锯齿
        idTvEq.setText(Html.fromHtml("疑问解答"));
        idTvEq.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putString(AppConst.WEBTYPE, AppConst.TYPE_QA);
                openActivity(CommWebActivity.class, bundle);
            }
        });

        idTvBuy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putString(AppConst.WEBTYPE, AppConst.COMWEB_XIUMI);
                openActivity(CommWebActivity.class, bundle);
            }
        });
    }

    @Override
    protected void afterCreate(Bundle bundle) {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(MineXiuMiActivity.this, LinearLayoutManager.VERTICAL, false);

        List<PositonBean> mlist = new ArrayList<>();
        PositonBean positonBean = new PositonBean();
        positonBean.setName("76");
        positonBean.setSelect(true);

        PositonBean positonBean1 = new PositonBean();
        positonBean1.setName("88");
        positonBean1.setSelect(false);


        PositonBean positonBean2 = new PositonBean();
        positonBean2.setName("10000");
        positonBean2.setSelect(false);
        mlist.add(positonBean);
        mlist.add(positonBean1);
        mlist.add(positonBean2);

        SelectXiuMiAdapter selectXiuMiAdapter = new SelectXiuMiAdapter(this, mlist, R.layout.item_select_xiumi);
        //videoEditAdapter.add
        lsRecord.setLayoutManager(linearLayoutManager);
        lsRecord.setAdapter(selectXiuMiAdapter);

        selectXiuMiAdapter.setonItemClickListener(new onItemClickListener() {
            @Override
            public void onItemClickListener(int position) {
                for (PositonBean positon : mlist) {
                    positon.setSelect(false);
                }
                mlist.get(position).setSelect(true);
                selectXiuMiAdapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    public void loadSucceed(String result) {

    }

    @Override
    public void loadFaild(String error) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
    }


    public interface onItemClickListener {
        void onItemClickListener(int position);
    }

    public class SelectXiuMiAdapter extends ABaseRefreshAdapter<PositonBean> {

        private onItemClickListener listener;

        public SelectXiuMiAdapter(Context context, List<PositonBean> datas, int itemLayoutId) {
            super(context, datas, itemLayoutId);
        }

        @Override
        protected void convert(BaseRecyclerHolder baseRecyclerHolder, PositonBean s, int i) {
            RelativeLayout linearLayout = baseRecyclerHolder.getView(R.id.rl_layout);
            ImageView imageTag = baseRecyclerHolder.getView(R.id.iv_selected_yes);
            TextView textView = baseRecyclerHolder.getView(R.id.tv_name);
            if (s.isSelect()) {
                linearLayout.setBackgroundResource(R.drawable.filter_selected_style);
            } else {
                linearLayout.setBackgroundResource(R.drawable.shape_make_edit_background);
            }
            imageTag.setVisibility(s.isSelect() ? View.VISIBLE : View.GONE);
            textView.setText(s.getName());
            linearLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        listener.onItemClickListener(i);
                    }
                }
            });

        }

        public void setonItemClickListener(onItemClickListener listener) {
            this.listener = listener;
        }

    }

    public class PositonBean {
        String name;
        boolean isSelect;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public boolean isSelect() {
            return isSelect;
        }

        public void setSelect(boolean select) {
            isSelect = select;
        }

    }
}
